<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Web高级开发 | Advanced Web Development Course</title>
    <link
      href="https://fonts.googleapis.com/css?family=Heebo:300,400"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="../../css/main.css" />
    <link rel="stylesheet" href="../../css/tabs.css" />
    <script src="../../js/uikit.js"></script>
    <link href="../../css/prism.css" rel="stylesheet" />
    <script src="../../js/prism.js"></script>
  </head>

  <body>
    <div
      data-uk-sticky="animation: uk-animation-slide-top; sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; cls-inactive: uk-navbar-transparent; top: 300"
    >
      <nav class="uk-navbar-container pt-2 pb-2">
        <div class="uk-container">
          <div data-uk-navbar>
            <div class="uk-navbar-left">
              <a
                class="uk-navbar-item uk-logo uk-visible@m"
                href="../../index.html"
                >Web高级开发课程</a
              >
              <a
                class="uk-navbar-tuk-containeroggle uk-hidden@m"
                href="#offcanvas-docs"
                data-uk-toggle
                ><span data-uk-navbar-toggle-icon></span>
                <span class="uk-margin-small-left">目录</span></a
              >
              <ul class="uk-navbar-nav uk-visible@m">
                <li><a href="../../index.html">文档</a></li>
              </ul>
            </div>
            <div class="uk-navbar-center uk-hidden@m">
              <a class="uk-navbar-item uk-logo" href="../../index.html"
                >Web高级开发课程</a
              >
            </div>
          </div>
        </div>
      </nav>
    </div>

    <div class="uk-section">
      <div class="uk-container">
        <div class="uk-grid-large" data-uk-grid>
          <div class="sidebar-fixed-width uk-visible@m">
            <div class="sidebar-docs uk-position-fixed uk-margin-top">
              <h5>关于这门课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../../index.html">大纲</a></li>
              </ul>
              <h5>章节</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../01.html">第1章 - Spring基础</a></li>
                <li><a href="../02.html">第2章 - Spring中的Bean</a></li>
                <li><a href="../03.html">第3章 - Spring AOP</a></li>
                <li><a href="../04.html">第4章 - Spring的事物管理</a></li>
                <li><a href="../05.html">第5章 - 初识MyBatis</a></li>
                <li><a href="../06.html">第6章 - MyBatis的核心配置</a></li>
                <li><a href="../07.html">第7章 - MyBatis的高级应用</a></li>
                <li><a href="../08.html">第8章 - Spring MVC入门</a></li>
                <li><a href="../09.html">第9章 - Spring MVC数据绑定</a></li>
                <li><a href="../10.html">第10章 - Spring MVC拦截器</a></li>
                <li><a href="../11.html">第11章 - Spring Boot基础</a></li>
                <li><a href="../12.html">第12章 - Spring Boot的Web开发</a></li>
                <li>
                  <a href="../13.html">第13章 - Spring Boot的数据库连接</a>
                </li>
                <li><a href="../14.html">第14章 - 框架整合</a></li>
              </ul>
              <h5>实验课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="./code-01.html">01</a></li>
                <li><a href="./code-02.html">02</a></li>
                <li><a href="./code-03.html">03</a></li>
                <li><a href="./code-04.html">04</a></li>
                <li><a href="./code-05.html">05</a></li>
                <li><a href="./code-06.html">06</a></li>
                <li><a href="./code-07.html">07</a></li>
                <li><a href="./code-08.html">08</a></li>
                <li><a href="./code-09.html">09</a></li>
                <li><a href="./code-10.html">10</a></li>
                <li class="uk-active"><a href="./code-11.html">11</a></li>
                <li><a href="./code-12.html">12</a></li>
              </ul>
            </div>
          </div>
          <div class="uk-width-1-1 uk-width-expand@m">
            <article class="uk-article">
              <h1>实验课11 - Spring Boot + Mybatis 2</h1>

              <h3>* 学校实验室配置</h3>
              <p>如果想在学校实验室M5 210的机器上运行，需要改几个地方，如果是自己的电脑，可以跳过这里</p>
              <ol>
              <li><p>在application.properties 文件中, 把</p>
              <p>spring.datasource.driver-class-name和spring.datasource.url的值分别设成。</p>
              <p>注意这里的databaseName=users中的users是你自己建的数据库的名字，这个可以是任意的</p>
<pre><code class='language-xml' lang='xml'>spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost:1433;integratedSecurity=true;databaseName=users
</code></pre>
              </li>
              <li><p>pom.xml文件：</p>
              <p>在dependencies标签中加入</p>
<pre><code class='language-xml' lang='xml'>&lt;dependency&gt;
    &lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt;
    &lt;artifactId&gt;sqljdbc4&lt;/artifactId&gt;
    &lt;version&gt;4.0.0&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
              <p>&nbsp;</p>
              </li>
              
              </ol>
              
            
        <div>
          <h3>1.书接上回</h3>
          <p>通过配置的方式，做删除和更新操作，在UserMapper.xml文件中加入</p>
<pre><code class='language-xml' lang='xml'>&lt;update id=&quot;updateUser&quot; parameterType=&quot;com.qust.pojo.User&quot;&gt;
  UPDATE tb_user SET username=#{username}, password=#{password} WHERE id=#{id}
&lt;/update&gt;

&lt;delete id=&quot;deleteUserById&quot;&gt;
  DELETE FROM tb_user WHERE id=#{id}
&lt;/delete&gt;
</code></pre>
<p>通过注解的方式，做增删改查的操作</p>
<p>定义一个AnotherUserMapper.java</p>
<pre><code class='language-java' lang='java'>package com.qust.mapper;
          
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.qust.pojo.User;

@Mapper
public interface AnotherUserMapper {
  
  @Select(&quot;select * from tb_user&quot;)
  List&lt;User&gt; anotherFindAll();
  
  @Insert(&quot;insert into tb_user (username) values(#{username})&quot;)
  int insertUser(User user);
  
  @Delete(&quot;delete from tb_user where id=#{id}&quot;)
  int anotherDeleteUserById(Integer id);
  
  @Update(&quot;update tb_user set username=#{username} where id=#{id}&quot;)
  int anotherUpdateUser(User user);

}
</code></pre>
          <p>然后再在service的接口，service实现类以及controller里定义相应的方法</p>
          <p>UserService.java</p>
<pre><code class='language-java' lang='java'>package com.qust.service;

import java.util.List;

import com.qust.pojo.User;

public interface UserService {
  List&lt;User&gt; findAll();
  List&lt;User&gt; anotherFindAll();
  
  int addUser(User user);
  int insertUser(User user);
  
  int updateUser(User user);
  int anotherUpdateUser(User user);
  
  int deleteUserById(Integer id);
  int anotherDeleteUserById(Integer id);
}

</code></pre>
          <p>UserServiceImpl.java</p>
<pre><code class='language-java' lang='java'>package com.qust.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qust.mapper.AnotherUserMapper;
import com.qust.mapper.UserMapper;
import com.qust.pojo.User;
import com.qust.service.UserService;

@Service(&quot;userService&quot;)
public class UserServiceImpl implements UserService{

  @Autowired
  private UserMapper userMapper;
  
  @Autowired
  AnotherUserMapper anotherUserMapper;
  
  @Override
  public List&lt;User&gt; findAll() {
    return userMapper.findAll();
  }

  @Override
  public int addUser(User user) {
    return userMapper.addUser(user);
  }

  @Override
  public int updateUser(User user) {
    return userMapper.updateUser(user);
  }

  @Override
  public int deleteUserById(Integer id) {
    return userMapper.deleteUserById(id);
  }
  
  @Override
  public List&lt;User&gt; anotherFindAll() {
    return anotherUserMapper.anotherFindAll();
  }

  
  @Override
  public int insertUser(User user) {
    return anotherUserMapper.insertUser(user);
  }
  
  @Override
  public int anotherDeleteUserById(Integer id) {
    return anotherUserMapper.anotherDeleteUserById(id);
  }
  
  @Override
  public int anotherUpdateUser(User user) {
    return anotherUserMapper.anotherUpdateUser(user);
  }
}

</code></pre>
          <p>UserController.java</p>
<pre><code class='language-java' lang='java'>package com.qust.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qust.pojo.User;
import com.qust.service.UserService;

@Controller
public class UserController {
  
  @Autowired
  UserService userService;
  
  @GetMapping(&quot;/findAll&quot;)
  @ResponseBody
  public List&lt;User&gt; findAll(){
    return userService.findAll();
  }
  
  @GetMapping(&quot;/anotherFindAll&quot;)
  @ResponseBody
  public List&lt;User&gt; anotherFindAll(){
    return userService.anotherFindAll();
  }
  
  @PostMapping(&quot;/addUser&quot;)
  @ResponseBody
  public int addUser(User user) {
    return userService.addUser(user);
  }
  
  @PostMapping(&quot;/insertUser&quot;)
  @ResponseBody
  public int insertUser(User user) {
    return userService.insertUser(user);
  }
  
  
  @PostMapping(&quot;/deleteUser&quot;)
  @ResponseBody
  public int deleteUser(int id) {
    return userService.deleteUserById(id);
  }
  
  @PostMapping(&quot;/deleteUserById&quot;)
  @ResponseBody
  public int anotherDeleteUserById(int id) {
    return userService.anotherDeleteUserById(id);
  }
  
  
  @PostMapping(&quot;/updateUser&quot;)
  @ResponseBody
  public int updateUser(User user) {
    return userService.updateUser(user);
  }
  
  
  @PostMapping(&quot;/anotherUpdateUser&quot;)
  @ResponseBody
  public int anotherUpdateUser(User user) {
    return userService.anotherUpdateUser(user);
  }
  
}
</code></pre>
          <p>之后在浏览器或者postman中做相应的请求即可。</p>
          <h3>2.整合到前端</h3>
          <h4>1.删除用户</h4>
          <p>在前端模板文件(.html)中加入如下的代码，让每个用户后面跟着一个删除按钮</p>
          <p>注意这里的路径：/deleteUser/${user.id}</p>
<pre><code class='language-html' lang='html'>&lt;div th:each=&quot;user : ${userList}&quot; style=&quot;display:flex;&quot;&gt;
&lt;div th:text=&quot;${user.username}&quot; style=&quot;margin-right: 10px;&quot;&gt;&lt;/div&gt;
    &lt;form action=&quot;#&quot; th:action=&quot;@{/deleteUser/}+${user.id}&quot; method=&quot;post&quot; &gt;
        &lt;input th:type=&quot;hidden&quot; type=&quot;text&quot; name=&quot;username&quot; th:value=&quot;${user.id}&quot; placeholder=&quot;id&quot; /&gt;
        &lt;input type=&quot;submit&quot; value=&quot;删除&quot; /&gt;
    &lt;/form&gt;
&lt;/div&gt;
</code></pre>
<p>然后在controller文件中定义相应的请求及方法</p>
<p>这里的/deleteUser/{id}和上面的/deleteUser/${user.id}是相互对应的</p>
<p>@PathVariable(&quot;id&quot;）是为了从路径中的{id}获得实际的id值，从而传入给deteleUserById方法</p>
<p>return “redirect:/mingyuan” 是为了返回主页</p>
<pre><code class='language-java' lang='java'>@PostMapping(&quot;/deleteUser/{id}&quot;)
public String deteleUserById(@PathVariable(&quot;id&quot;) Integer id) {
    userService.deleteUserById(id);
    return &quot;redirect:/mingyuan&quot;;
}
</code></pre>
          <h4>2.添加用户</h4>
          <p>在前端模板文件(.html)中加入如下的代码</p>
<pre><code class='language-html' lang='html'>&lt;form action=&quot;#&quot; th:action=&quot;@{/addNewUser}&quot; th:object=&quot;${user}&quot; method=&quot;post&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; placeholder=&quot;名字&quot; /&gt;
    &lt;input type=&quot;submit&quot; value=&quot;添加&quot; /&gt;
&lt;/form&gt;
</code></pre>
<p>在controller里面加入</p>
<pre><code class='language-java' lang='java'>@PostMapping(&quot;/addNewUser&quot;)
public String addUser(User user) {
    userService.addUser(user);
    return &quot;redirect:/mingyuan&quot;;
}
</code></pre>
<p>这样在填入用户名并且每次点击“添加”按钮的时候，就会有新的用户添加到了数据库当中。</p>
<p>&nbsp;</p>
          
            <p>
              上课的源码在<a href="https://gitee.com/lqin2333/springboot" target="_blank">这里</a>（← 戳它下载）
            </p>

        </div>



            </article>
          </div>
        </div>
      </div>
    </div>

    <div id="offcanvas-docs" data-uk-offcanvas="overlay: true">
      <div class="uk-offcanvas-bar">
        <button class="uk-offcanvas-close" type="button" data-uk-close></button>
        <h5 class="uk-margin-top">关于这门课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../indext.html">大纲</a></li>
        </ul>
        <h5 class="uk-margin-top">章节</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../01.html">第1章 - Spring基础</a></li>
          <li><a href="../02.html">第2章 - Spring中的Bean</a></li>
          <li><a href="../03.html">第3章 - Spring AOP</a></li>
          <li><a href="../04.html">第4章 - Spring的事物管理</a></li>
          <li><a href="../05.html">第5章 - 初识MyBatis</a></li>
          <li><a href="../06.html">第6章 - MyBatis的核心配置</a></li>
          <li><a href="../07.html">第7章 - MyBatis的高级应用</a></li>
          <li><a href="../08.html">第8章 - Spring MVC入门</a></li>
          <li><a href="../09.html">第9章 - Spring MVC数据绑定</a></li>
          <li><a href="../10.html">第10章 - Spring MVC拦截器</a></li>
          <li><a href="../11.html">第11章 - Spring Boot基础</a></li>
          <li><a href="../12.html">第12章 - Spring Boot的Web开发</a></li>
          <li><a href="../13.html">第13章 - Spring Boot的数据库连接</a></li>
          <li><a href="../14.html">第14章 - 框架整合</a></li>
        </ul>
        <h5>实验课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="./code-01.html">01</a></li>
          <li><a href="./code-02.html">02</a></li>
          <li><a href="./code-03.html">03</a></li>
          <li><a href="./code-04.html">04</a></li>
          <li><a href="./code-05.html">05</a></li>
          <li><a href="./code-06.html">06</a></li>
          <li><a href="./code-07.html">07</a></li>
          <li><a href="./code-08.html">08</a></li>
          <li><a href="./code-09.html">09</a></li>
          <li><a href="./code-10.html">10</a></li>
          <li class="uk-active"><a href="./code-11.html">11</a></li>
          <li><a href="./code-12.html">12</a></li>
        </ul>
      </div>
    </div>

    <footer class="uk-section uk-text-center uk-text-muted">
      <div class="uk-container uk-container-small">
        <div class="uk-margin-medium"></div>
      </div>
    </footer>

    <script src="../js/awesomplete.js"></script>
    <script src="../js/custom.js"></script>
  </body>
</html>
